<?php

namespace App\Models\Admin;

use App\Http\Controllers\Admin\Traits\AdminTrait;
use App\Log\CompanyLog;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;

class Agents extends Base
{
    const OPERATE_PID = 1;
    const MODEL       = 'agents';

    use AdminTrait;

    protected $table      = 'agents';
    protected $guarded    = [];
    protected $dateFormat = 'U';

    public static function list($data = [], $skip = 0, $size = 10)
    {
        $select = self::where([]);
        if (!empty($data['agent_name'])) {
            $select = $select->where('agent_name', 'like', '%' . $data['agent_name'] . '%');
        }

        if (!empty($data['domain_name'])) {
            $select = $select->where('domain_name', $data['domain_name']);
        }

        $count = $select->count();
        $list  = $select->skip($skip)->take($size)
            ->orderBy('created_at', 'desc')
            ->select(['*'])
            ->get();

        return ['list' => $list, 'count' => $count];
    }

    // 添加操作日志
    public static function addLog($pre, $next, $type, $method = '')
    {
        $info = AdminOperateModule::where('pid', self::OPERATE_PID)->where('type', $type)->where('model', self::MODEL)->when($method, function ($query) use ($method) {
            $query->where('method', $method);
        })->first();
        if (!empty($info)) {
            $adminLog = new CompanyLog($pre, $next, $info);
            $adminLog->addLog();
        }
    }
}
